package com.ruoyi.mymod.service.impl;

import com.ruoyi.mymod.service.IMessageService;
import com.ruoyi.mymod.service.IFeedAccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;

/**
 * 菜单 业务层处理
 *
 * @author ruoyi
 */
@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    IFeedAccessService feedAccessService;


    public static final String PREMISSION_STRING = "perms[\"{0}\"]";

    @Override
    public String selectMessage(String usercode,String acode,Integer alevel,String fcode,Integer flevel) throws Exception {
        String url = "jdbc:oracle:thin:@10.104.8.7:1521:orcl"; // 使用服务名
        String user = "psi";
        String password = "psi";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String message="[[\"外销\",\"0\",\"0\",\"0\",\"0\",\"0\"],[\"内销\",\"0\",\"0\",\"0\",\"0\",\"0\"],[\"总计\",\"0\"," +
                "\"0\",\"0\",\"0\",\"0\"]]";
//        try {
            // 加载Oracle JDBC驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");

            // 建立数据库连接
            conn = DriverManager.getConnection(url, user, password);

            // 创建Statement对象
            stmt = conn.createStatement();

            // 执行SQL查询
            String sql = "select ctcnfd.ai.genmsg('"+usercode+"',trunc(sysdate-2),"+alevel+",'"+acode+"',"+flevel+",'"+fcode+"') from dual";
            rs = stmt.executeQuery(sql);

            // 处理查询结果
            if (rs.next()){
                message=rs.getString(1);
            }

            if (message==null){
                message="[[\"外销\",\"0\",\"0\",\"0\",\"0\",\"0\"],[\"内销\",\"0\",\"0\",\"0\",\"0\",\"0\"],[\"总计\"," +
                        "\"0\",\"0\",\"0\",\"0\",\"0\"]]";
            }

//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        finally {
//            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
//        }

       feedAccessService.accessRecord(usercode,acode,alevel,fcode,flevel);


        return message;
    }


}
